package com.tangdi.Action;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.tangdi.Utils;
import com.tangdi.importdata.Import_GZ_Data;

public class GZ_Data_ImportAction  extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(GZ_Data_ImportAction.class);
	public GZ_Data_ImportAction() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		String year_month =request.getParameter("year_month");
		String return_result = "导入成功！";

		HttpSession session=request.getSession();
		if(year_month != null && type.equals("2")){
			Import_GZ_Data imp_data = new Import_GZ_Data();
			int rs_status = -1;
			String import_path = Utils.getFilePath("import_path");
			String[] filePaths = {
									import_path  + "/" + year_month + "/" + "CZGZ_BZGZXM" + "_" + year_month + ".txt",
									import_path  + "/" + year_month + "/" + "CZGZ_SJGZ" + "_" + year_month + ".txt",
									import_path  + "/" + year_month + "/" + "CZGZ_DWXX" + "_" + year_month + ".txt",
									import_path  + "/" + year_month + "/" + "CZGZ_GZLB" + "_" + year_month + ".txt",
									import_path  + "/" + year_month + "/" + "CZGZ_ZGGZMX" + "_" + year_month + ".txt"
								 };
			/*String[] filePaths = {"D:/桌面文件/参与项目/南京交行数据打印系统/import_data/" + year_month + "/" + year_month + "_DWXX.txt"
					 			  };*/
		    for(int i = 0; i < filePaths.length; i++){
		    	File file = new File(filePaths[i]);
				if(!file.exists()){
					logger.info(filePaths[i] + "文件不存在！");
					return_result = filePaths[i] + "文件不存在！";
					break;
				}
				if(filePaths[i].contains("CZGZ_GZLB")){//工资类别
					rs_status = imp_data.import_GZLB(filePaths[i], year_month);
					if(rs_status != 0){
						logger.info(filePaths[i] + "导入失败！");
						return_result = filePaths[i] + "导入失败！";
						break;
						}
				}
				if(filePaths[i].contains("CZGZ_SJGZ")){//实际工资科目表
					rs_status = imp_data.import_SJGZ(filePaths[i], year_month);
					if(rs_status != 0){
						logger.info(filePaths[i] + "导入失败！");
						return_result = filePaths[i] + "导入失败！";
						break;
						}
				}
				if(filePaths[i].contains("CZGZ_DWXX")){//单位工资汇总表
					rs_status = imp_data.import_GZDWXX(filePaths[i], year_month);
					if(rs_status != 0){
						logger.info(filePaths[i] + "导入失败！");
						return_result = filePaths[i] + "导入失败！";
						break;
						}
				}
				if(filePaths[i].contains("CZGZ_ZGGZMX")){//工资明细表
					rs_status = imp_data.import_GZMX(filePaths[i], year_month);
					if(rs_status != 0){
						logger.info(filePaths[i] + "导入失败！");
						return_result = filePaths[i] + "导入失败！";
						break;
						}
				}
/*				if(filePaths[i].contains("CZGZ_BZGZXM")){//
					rs_status = imp_data.import_GZMX(filePaths[i], year_month);
					if(rs_status != 0){
						return_result = filePaths[i] + "导入失败！";
						break;
						}
				}*/
		    }
		    session.setAttribute("rs_status", String.valueOf(rs_status));
			session.setAttribute("return_result", return_result);
			session.setAttribute("year_month", year_month);	
		}else{
		    session.setAttribute("rs_status", null);
			session.setAttribute("return_result", null);
			session.setAttribute("year_month", null);			
		}
		
		response.sendRedirect("gz_data_import.jsp");
		
	}
}